package cc.rengu.igas.mcps.core.service.trans.scanpay;

import cc.rengu.igas.mcps.common.constant.McpsTreeNodeConstant;
import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.service.base.B2CManageService;
import cc.rengu.igas.mcps.facade.request.GenDynamicQrCodeRequest;
import cc.rengu.igas.mcps.facade.response.GenDynamicQrCodeResponse;
import cc.rengu.igas.share.core.model.MchntAcctInfo;
import cc.rengu.igas.share.core.model.MchntAuthInfo;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.model.MchntTermInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.BizStatusEnum;
import cc.rengu.oltp.service.common.enums.OrderStatusEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.common.enums.TransTypeEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 扫码支付（生成动态收款码）
 */
public class GenDynamicQrCodeService extends B2CManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        GenDynamicQrCodeRequest genDynamicQrCodeRequest = new GenDynamicQrCodeRequest();
        ConvertUtil.convertOutput(genDynamicQrCodeRequest);
        return genDynamicQrCodeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BizResponse bizResponse = new BizResponse();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        GenDynamicQrCodeRequest genDynamicQrCodeRequest = (GenDynamicQrCodeRequest) request;
        ConvertUtil.convertOutput(genDynamicQrCodeRequest);

        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String instId = genDynamicQrCodeRequest.getHeader().getInstId();
        String mchntNo = genDynamicQrCodeRequest.getMchntNo();
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(instId, mchntNo);
        if (null == mchntInfo) {
            rglog.error("查询商户信息失败<{}>", mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /*通过系统参数控制银联标准二维码还是收单机构动态二维码*/
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, "MCHNT_SERVICE", "dynamicQrCodeType");
        SysParam sysParamType = sysParamService.getSysParamInfo(instId, "MCHNT_SERVICE", "qrCodeType");
        if (null != sysParam && null != sysParam.getParamValue() && null != sysParamType && null != sysParamType.getParamValue()) {
            if ("CUPS".equals(sysParam.getParamValue()) && AppParamConstant.YES.equals(sysParam.getParamStatus()) && "CUPS".equals(sysParamType.getParamValue()) && AppParamConstant.YES.equals(sysParamType.getParamStatus())) {
                /*生成标准银联聚合二维码*/
                bizResponse = directGenQrCodeService(genDynamicQrCodeRequest, sysSeqNum, mchntInfo);
            } else {
                /*生成本行收单码*/
                bizResponse = genUpqcQrCode(genDynamicQrCodeRequest, sysSeqNum, mchntInfo);
            }
        } else {
            /*生成本行聚合码*/
            bizResponse = genUpqcQrCode(genDynamicQrCodeRequest, sysSeqNum, mchntInfo);
        }
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GenDynamicQrCodeResponse genDynamicQrCodeResponse = (GenDynamicQrCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(genDynamicQrCodeResponse);
    }

    /**
     * 银联标准的聚合二维码
     *
     * @param request
     * @return
     * @throws Exception
     */
    private BizResponse genUpqcQrCode(GenDynamicQrCodeRequest request, String sysSeqNum, MchntInfo mchntInfo) throws Exception {
        BizResponse<GenDynamicQrCodeResponse> bizResponse = new BizResponse<>();
        GenDynamicQrCodeResponse genDynamicQrCodeResponse = new GenDynamicQrCodeResponse();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = request.getHeader().getInstId();
        String termNo = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.TERM_NO);
        /*生成用户订单表*/
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        ConvertUtil.convertOutput(userOrderInfo);
        userOrderInfo.setMsgSrcId(request.getHeader().getSrcSysId());
        userOrderInfo.setTransChannelId(this.txnInfo.getSrcTxnAuthCfg().getTransChannelId());
        userOrderInfo.setTxnDate(DateUtil.getCurrentDate());
        userOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        userOrderInfo.setOrderDesc(request.getGoodDesc());
        userOrderInfo.setOrderCcy(request.getTransCcy());
        if (!StringUtil.isEmptyOrNull(request.getTransAmt())) {
            userOrderInfo.setOrderAmt(request.getTransAmt());
        }
        userOrderInfo.setNeedPayAmt(userOrderInfo.getOrderAmt());
        userOrderInfo.setRealPayAmt(userOrderInfo.getOrderAmt());
        userOrderInfo.setUserOrderId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        userOrderInfo.setMchntOrderId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        /*订单创建时间*/
        String orderCreateTime = DateUtil.getCurrentDateTime("yyyyMMddHHmmss");
        userOrderInfo.setOrderCreateTime(orderCreateTime);
        userOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        userOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        userOrderInfo.setBizStatus(BizStatusEnum.INIT.getStatus());

        /*订单附加信息*/
        Map<String, String> orderExtraInfo = new HashMap<String, String>();
        orderExtraInfo.put("qrCodeType", "1");
        userOrderInfo.setOrderExtraInfo(JSON.toJSONString(orderExtraInfo));
        /*生成商户订单*/
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        /*内部树复制会把订单状态保存到商户订单信息表*/
        BeanUtil.beanCopy(userOrderInfo, mchntOrderInfo);
        /*设置商户订单表中业务产品代码*/
        mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
        mchntOrderInfo.setTermNo(termNo);
        mchntOrderInfo.setTermType(request.getTermInfo().getDeviceType());
        mchntOrderInfo.setMchntNo(request.getMchntNo());
        /*若存在 设置连锁商户门店*/
        if (null != mchntInfo.getChainMchntNo()) {
            mchntOrderInfo.setChainMchntNo(mchntInfo.getChainMchntNo());
        }
        mchntOrderInfo.setIndustryType(mchntInfo.getIndustryType());
        mchntOrderInfo.setMchntName(mchntInfo.getMchntName());
        mchntOrderInfo.setAcqInstCode(mchntInfo.getSignInstId());
        mchntOrderInfo.setRealSettleAmt(request.getTransAmt());
        /*获取结算账户信息*/
        MchntAcctInfo mchntAcctInfo = getMchntAcctInfo(request, mchntInfo);
        mchntOrderInfo.setRealSettleType(mchntAcctInfo.getSettleType());
        mchntOrderInfo.setRealSettleFlag("N");

        //查询终端信息表，获取终端布放地理位置
        MchntService mchntService = new MchntServiceImpl();
        MchntTermInfo mchntTermInfo = mchntService.getMchntTermInfo(instId,request.getMchntNo(),request.getTermNo());
        String areaInfo = mchntTermInfo.getTermAreaCode();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("termInfo", request.getTermInfo());
        jsonObject.put("areaInfo", areaInfo);
        if (StringUtil.isEmptyOrNull(mchntOrderInfo.getOrderExtraInfo())) {
            mchntOrderInfo.setOrderExtraInfo(jsonObject.toJSONString());
        } else {
            jsonObject = JSONObject.parseObject(mchntOrderInfo.getOrderExtraInfo());
            jsonObject.put("termInfo", request.getTermInfo());
            jsonObject.put("areaInfo", areaInfo);
            mchntOrderInfo.setOrderExtraInfo(jsonObject.toJSONString());

        }

        /*设置订单有效时间根据上送的有效时间，以分钟计算，如果上送按上送计算，如果未上送默认一分钟*/
        Long futuretimes = new Long("0");
        /*默认3分钟时间*/
        futuretimes = System.currentTimeMillis() + 3 * 60 * 1000;
        String epiryTime = DateUtil.getDateByTimeStamp(futuretimes, "yyyyMMddHHmmss");
        userOrderInfo.setOrderExpiryTime(epiryTime);

        /*根据业务权限组权限控制*/
        /*获取扫码支持权限*/
        String mchntNo = request.getMchntNo();
        if (null != mchntInfo.getIndependentDataFlag()) {
            if ("11".equals(mchntInfo.getMchntType()) && "N".equals(mchntInfo.getIndependentDataFlag())) {
                mchntNo = mchntInfo.getChainMchntNo();
            }
        }
        List<MchntAuthInfo> mchntAuthInfoList = mchntService.getMchntAuthInfo(instId, mchntNo, null);
        if (null == mchntAuthInfoList) {
            rglog.debug("商户未配置业务权限");
            throw new BizException(RespCodeEnum.MCHNT_AUTH_CTRL_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_AUTH_CTRL_INFO_NOT_FOUND.getRespDesc());
        }
        /*查询商户支持产品业务权限*/
        String supportScanQrCodeType = null;
        /*获取扫码权限*/
        List<MchntAuthInfo> scanQrCodeMchntAuthCtrlList = mchntAuthInfoList.stream().filter(item -> "02".equals(item.getBaseProdCode())
                && item.getTxnNum().substring(1, 3).equals(TransTypeEnum.PAY.getTransType())).collect(Collectors.toList());
        /*判断微信通道 根据业务细分区分扫码对应的通道*/
        List<MchntAuthInfo> wxScanQrCodeMchntAuthCtrlList = scanQrCodeMchntAuthCtrlList.stream().filter(item -> "00".equals(item.getBizType())).collect(Collectors.toList());
        if (!wxScanQrCodeMchntAuthCtrlList.isEmpty()) {
            /*开通微信扫码权限*/
            supportScanQrCodeType = "01";
        }
        List<MchntAuthInfo> aliScanQrCodeMchntAuthCtrlList = scanQrCodeMchntAuthCtrlList.stream().filter(item -> "01".equals(item.getBizType())).collect(Collectors.toList());
        if (!aliScanQrCodeMchntAuthCtrlList.isEmpty()) {
            /*开通支付宝码权限*/
            if (StringUtil.isEmptyOrNull(supportScanQrCodeType)) {
                supportScanQrCodeType = "04";
            } else {
                supportScanQrCodeType += "|04";
            }
        }
        List<MchntAuthInfo> upqcScanQrCodeMchntAuthCtrlList = scanQrCodeMchntAuthCtrlList.stream().filter(item -> "02".equals(item.getBizType())).collect(Collectors.toList());
        if (!upqcScanQrCodeMchntAuthCtrlList.isEmpty()) {
            /*开通银联二维码权限置10位1*/
            if (StringUtil.isEmptyOrNull(supportScanQrCodeType)) {
                supportScanQrCodeType = "05";
            } else {
                supportScanQrCodeType += "|05";
            }
        }
        /*生成二维码URL串*/
        /* 1:获取url串 2:调加密机生成对商户订单号进行加密*/
        /*获取银联聚合码的域名*/
        String url = getSysParamYlValue(instId, "instDomainName");
        userOrderInfo.setOrderUrl(url + "inf=" + "D" + sysSeqNum);
        OrderService orderService = new OrderServiceImpl();
        List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
        mchntOrderInfoList.add(mchntOrderInfo);
        if (!orderService.registerOrderInfo(userOrderInfo, mchntOrderInfoList, null)) {
            rglog.error("订单入库失败 系统流水号{}", sysSeqNum);
            throw new BizException(RespCodeEnum.INSERT_USER_ORDER_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_USER_ORDER_INFO_ERROR.getRespDesc());
        }
        /* 设置返回报文*/
        genDynamicQrCodeResponse.setPayId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        genDynamicQrCodeResponse.setQrCode(userOrderInfo.getOrderUrl());
        genDynamicQrCodeResponse.setQrCodeType(supportScanQrCodeType);

        bizResponse.setResult(genDynamicQrCodeResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    /**
     * 收单发码
     *
     * @param request 扫码请求信息
     * @return 业务调用结果
     */
    BizResponse directGenQrCodeService(GenDynamicQrCodeRequest request, String sysSeqNum, MchntInfo mchntInfo) throws Exception {
        BizResponse<GenDynamicQrCodeResponse> bizResponse = new BizResponse<>();
        GenDynamicQrCodeResponse genDynamicQrCodeResponse = new GenDynamicQrCodeResponse();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String termNo = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.TERM_NO);
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        /*生成用户订单表*/
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        ConvertUtil.convertOutput(userOrderInfo);
        userOrderInfo.setMsgSrcId(request.getHeader().getSrcSysId());
        userOrderInfo.setTransChannelId(this.txnInfo.getSrcTxnAuthCfg().getTransChannelId());
        userOrderInfo.setTxnDate(DateUtil.getCurrentDate());
        userOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        userOrderInfo.setOrderDesc(request.getGoodDesc());
        userOrderInfo.setOrderCcy(request.getTransCcy());
        if (!StringUtil.isEmptyOrNull(request.getTransAmt())) {
            userOrderInfo.setOrderAmt(request.getTransAmt());
        }
        userOrderInfo.setNeedPayAmt(userOrderInfo.getOrderAmt());
        userOrderInfo.setRealPayAmt(userOrderInfo.getOrderAmt());
        userOrderInfo.setMchntOrderId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        userOrderInfo.setUserOrderId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        userOrderInfo.setOrderCreateTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
        userOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        userOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        userOrderInfo.setBizStatus(BizStatusEnum.INIT.getStatus());
        /*订单附加信息*/
        Map<String, String> orderExtraInfo = new HashMap<String, String>();
        orderExtraInfo.put("qrCodeType", "1");
        userOrderInfo.setOrderExtraInfo(JSON.toJSONString(orderExtraInfo));
        /*生成商户订单*/
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        /*内部树复制会把订单状态保存到商户订单信息表*/
        BeanUtil.beanCopy(userOrderInfo, mchntOrderInfo);
        /*设置商户订单表中业务产品代码*/
        mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
        mchntOrderInfo.setTermNo(termNo);
        mchntOrderInfo.setTermType(request.getTermInfo().getDeviceType());
        mchntOrderInfo.setMchntNo(request.getMchntNo());
        mchntOrderInfo.setOrderAmt(request.getTransAmt());
        mchntOrderInfo.setIndustryType(mchntInfo.getIndustryType());
        mchntOrderInfo.setMchntName(mchntInfo.getMchntName());
        mchntOrderInfo.setAcqInstCode(mchntInfo.getSignInstId());
        mchntOrderInfo.setRealSettleAmt(request.getTransAmt());
        /*获取结算账户信息*/
        MchntAcctInfo mchntAcctInfo = getMchntAcctInfo(request, mchntInfo);
        mchntOrderInfo.setRealSettleType(mchntAcctInfo.getSettleType());
        mchntOrderInfo.setRealSettleFlag("N");

        //查询终端信息表，获取终端布放地理位置
        MchntService mchntService = new MchntServiceImpl();
        MchntTermInfo mchntTermInfo = mchntService.getMchntTermInfo(instId, request.getMchntNo(), request.getTermNo());
        String areaInfo = mchntTermInfo.getTermAreaCode();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("termInfo", request.getTermInfo());
        jsonObject.put("areaInfo", areaInfo);
        if (StringUtil.isEmptyOrNull(mchntOrderInfo.getOrderExtraInfo())) {
            mchntOrderInfo.setOrderExtraInfo(jsonObject.toJSONString());
        } else {
            jsonObject = JSONObject.parseObject(mchntOrderInfo.getOrderExtraInfo());
            jsonObject.put("termInfo", request.getTermInfo());
            jsonObject.put("areaInfo", areaInfo);
            mchntOrderInfo.setOrderExtraInfo(jsonObject.toJSONString());
        }


        /*设置订单有效时间根据上送的有效时间，以分钟计算，如果上送按上送计算，如果未上送默认一分钟*/
        Long futuretimes = System.currentTimeMillis();
        /*默认3分钟时间*/
        futuretimes = futuretimes + 3 * 60 * 1000;
        String epiryTime = DateUtil.getDateByTimeStamp(futuretimes, "yyyyMMddHHmmss");
        userOrderInfo.setOrderExpiryTime(epiryTime);
        /*查询商户支持产品业务权限*/
        String applyIss = "00000000000000000000";
        /*根据业务权限组权限控制*/
        List<MchntAuthInfo> mchntAuthInfoList = mchntService.getMchntAuthInfo(instId, request.getMchntNo(), null);
        if (null == mchntAuthInfoList) {
            rglog.error("商户未配置业务权限");
            throw new BizException(RespCodeEnum.MCHNT_AUTH_CTRL_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_AUTH_CTRL_INFO_NOT_FOUND.getRespDesc());
        }
        List<MchntAuthInfo> scanQrCodeMchntAuthCtrlList = mchntAuthInfoList.stream().filter(item -> "02".equals(item.getBaseProdCode())
                && item.getTxnNum().substring(1, 3).equals(TransTypeEnum.PAY.getTransType())).collect(Collectors.toList());
        /*根据业务细分区分扫码对应的通道判断微信通道*/
        List<MchntAuthInfo> wxScanQrCodeMchntAuthCtrlList = scanQrCodeMchntAuthCtrlList.stream().filter(item -> AppParamConstant.DEFAULT_BIZ_TYPE.equals(item.getBizType())).collect(Collectors.toList());
        if (!wxScanQrCodeMchntAuthCtrlList.isEmpty()) {
            /*开通微信扫码权限第8位置1*/
            applyIss = applyIss.substring(0, 7) + "1" + applyIss.substring(8, applyIss.length());
        }
        List<MchntAuthInfo> aliScanQrCodeMchntAuthCtrlList = scanQrCodeMchntAuthCtrlList.stream().filter(item -> "01".equals(item.getBizType())).collect(Collectors.toList());
        if (!aliScanQrCodeMchntAuthCtrlList.isEmpty()) {
            /* 开通支付宝码权限置9位1*/
            applyIss = applyIss.substring(0, 8) + "1" + applyIss.substring(9, applyIss.length());
        }
        /*生成二维码URL串*/
        /* 1:获取url串 2:调加密机生成对商户订单号进行加密*/
        /*获取银联聚合码的域名*/
        String url = getSysParamYlValue(instId, "qrCodeDomainName");
        userOrderInfo.setOrderUrl(url + "/inf=" + "D" + sysSeqNum);
        OrderService orderService = new OrderServiceImpl();
        List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
        mchntOrderInfoList.add(mchntOrderInfo);
        if (!orderService.registerOrderInfo(userOrderInfo, mchntOrderInfoList, null)) {
            rglog.error("订单入库失败 系统流水号{}", sysSeqNum);
            throw new BizException(RespCodeEnum.INSERT_USER_ORDER_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_USER_ORDER_INFO_ERROR.getRespDesc());
        }
        rglog.info("用户订单信息表{}", JSON.toJSONString(userOrderInfo));

        genDynamicQrCodeResponse.setPayId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        genDynamicQrCodeResponse.setQrCode(userOrderInfo.getOrderUrl());
        genDynamicQrCodeResponse.setQrCodeType("01");
        bizResponse.setResult(genDynamicQrCodeResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }


    private String getSysParamYlValue(String instId, String paramKey) throws Exception {
        String sysParamValue = null;
        /*查询系统参数表*/
        String paramType = "MCHNT_SERVICE";
        /* key组成:instId+paramvalue+paramkey*/
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, paramType, paramKey);
        if (null != sysParam && null != sysParam.getParamValue()) {
            sysParamValue = sysParam.getParamValue();
        }
        return sysParamValue;
    }

    /*获取商户结算账户信息*/
    private MchntAcctInfo getMchntAcctInfo(GenDynamicQrCodeRequest request, MchntInfo mchntInfo) throws Exception {
        String mchntNo = mchntInfo.getMchntNo();
        /*判断商户是否为独立清算商户*/
        if (!"Y".equals(mchntInfo.getIndependentDataFlag()) && "11".equals(mchntInfo.getMchntType())) {
            mchntNo = mchntInfo.getChainMchntNo();
        }
        MchntService mchntService = new MchntServiceImpl();
        List<MchntAcctInfo> mchntAcctInfoList = mchntService.getMchntAcctInfo(request.getHeader().getInstId(), mchntNo);
        Optional<MchntAcctInfo> mchntAcctInfoOpt = mchntAcctInfoList.stream().filter(item -> item.getFundType().equals("00")).findFirst();
        return mchntAcctInfoOpt.orElse(null);
    }
}
